﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.PersonTypes.Authorization;
using LeadThing.AbpZeroTemplate.PersonTypes.Dtos;
using LeadThing.AbpZeroTemplate.Dto;



namespace LeadThing.AbpZeroTemplate.PersonTypes
{
    /// <summary>
    /// 人员类别服务实现
    /// </summary>
    public class PersonTypeAppService : AbpZeroTemplateAppServiceBase, IPersonTypeAppService
    {
        private readonly IRepository<PersonType, long> _personTypeRepository;
        private readonly IPersonTypeListExcelExporter _personTypeListExcelExporter;


        private readonly PersonTypeManage _personTypeManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public PersonTypeAppService(IRepository<PersonType, long> personTypeRepository,
PersonTypeManage personTypeManage
      , IPersonTypeListExcelExporter personTypeListExcelExporter
  )
        {
            _personTypeRepository = personTypeRepository;
            _personTypeManage = personTypeManage;
            _personTypeListExcelExporter = personTypeListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<PersonType> _personTypeRepositoryAsNoTrack => _personTypeRepository.GetAll().AsNoTracking();

        /// <summary>
        /// 获取全部人员分类
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<PersonTypeListDto>> GetAllPersonTypeListAsync()
        {
            var query = _personTypeRepositoryAsNoTrack;
            var items = await query.ToListAsync();
            var personTypeListDtos = items.MapTo<List<PersonTypeListDto>>();

            return new ListResultDto<PersonTypeListDto>(personTypeListDtos);
        }

        #endregion


        #region 人员类别管理
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType)]
        /// <summary>
        /// 根据查询条件获取人员类别分页列表
        /// </summary>
        public async Task<PagedResultDto<PersonTypeListDto>> GetPagedPersonTypesAsync(GetPersonTypeInput input)
        {

            var query = _personTypeRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件
            if (!string.IsNullOrWhiteSpace(input.Filter))
                query = query.Where(p => p.Name.Contains(input.Filter));


            var personTypeCount = await query.CountAsync();

            var personTypes = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var personTypeListDtos = personTypes.MapTo<List<PersonTypeListDto>>();
            return new PagedResultDto<PersonTypeListDto>(
            personTypeCount,
            personTypeListDtos
            );
        }

        /// <summary>
        /// 通过Id获取人员类别信息进行编辑或修改 
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType)]
        public async Task<GetPersonTypeForEditOutput> GetPersonTypeForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetPersonTypeForEditOutput();

            PersonTypeEditDto personTypeEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _personTypeRepository.GetAsync(input.Id.Value);
                personTypeEditDto = entity.MapTo<PersonTypeEditDto>();
            }
            else
            {
                personTypeEditDto = new PersonTypeEditDto();
            }

            output.PersonType = personTypeEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取人员类别ListDto信息
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType)]
        public async Task<PersonTypeListDto> GetPersonTypeByIdAsync(EntityDto<long> input)
        {
            var entity = await _personTypeRepository.GetAsync(input.Id);

            return entity.MapTo<PersonTypeListDto>();
        }







        /// <summary>
        /// 新增或更改人员类别
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType)]
        public async Task CreateOrUpdatePersonTypeAsync(CreateOrUpdatePersonTypeInput input)
        {
            if (input.PersonTypeEditDto.Id.HasValue)
            {
                await UpdatePersonTypeAsync(input.PersonTypeEditDto);
            }
            else
            {
                await CreatePersonTypeAsync(input.PersonTypeEditDto);
            }
        }

        /// <summary>
        /// 新增人员类别
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType_CreatePersonType)]
        public virtual async Task<PersonTypeEditDto> CreatePersonTypeAsync(PersonTypeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<PersonType>();

            entity = await _personTypeRepository.InsertAsync(entity);
            return entity.MapTo<PersonTypeEditDto>();
        }

        /// <summary>
        /// 编辑人员类别
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType_EditPersonType)]
        public virtual async Task UpdatePersonTypeAsync(PersonTypeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _personTypeRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _personTypeRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除人员类别
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType_DeletePersonType)]
        public async Task DeletePersonTypeAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _personTypeRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除人员类别
        /// </summary>
        //[AbpAuthorize(PersonTypeAppPermissions.PersonType_DeletePersonType)]
        public async Task BatchDeletePersonTypeAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _personTypeRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 人员类别的Excel导出功能


        public async Task<FileDto> GetPersonTypeToExcel()
        {
            var entities = await _personTypeRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<PersonTypeListDto>>();

            var fileDto = _personTypeListExcelExporter.ExportPersonTypeToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
